<?php

namespace app\schoolwall\controller;

use think\Controller;

class Posts extends Controller
{
    protected $PostsModel = null;
    protected $UserModel = null;
    protected $StarModel = null;
    protected $tallModel = null;

    public function _initialize()
    {
        $this->PostsModel = model('common/Posts/Posts');
        $this->UserModel = model('common/User/User');
        $this->StarModel = model('common/User/Star');
        $this->tallModel = model('common/Posts/Tall');
    }

    // 获取帖子列表
    public function getposts()
    {
        $id = $this->request->param('id', 0, 'trim'); //用户id
        $schoolid = $this->request->param('schoolid', '', 'trim');
        $page = $this->request->param('page', 1, 'trim');
        $limit = $this->request->param('limit', 10, 'trim');
        $userid = $this->request->param('userid', '', 'trim'); //用户id，点击的用户帖子

        $where = [];

        // if (!empty($schoolid)) {
        //     $where['schoolid'] = $schoolid;
        // }

        if (!empty($userid)) {
            $where['userid'] = $userid;
        }

        $user = $this->UserModel->find($id);
        if (!$user) {
            return $this->error('用户不存在');
        }

        $count = $this->PostsModel->where($where)->count();
        $item = $this->PostsModel->where($where)->with('user')->order('createtime desc')->page($page, $limit)->select();

        //循环$item，判断是否点赞过，是则在数组中添加is_like字段，值为1，否则为0
        foreach ($item as $key => $value) {
            $arr = explode(',', $value['likes']);
            if (in_array($id, $arr)) {
                $item[$key]['is_like'] = true;
            } else {
                $item[$key]['is_like'] = false;
            }
        }

        // 循环$item，判断是否收藏过，是则在数组中添加is_collect字段，值为1，否则为0
        foreach ($item as $key => $value) {
            $res = model('common/User/Star')->where('userid', $id)->where('postid', $value['id'])->find();
            if ($res) {
                $item[$key]['star'] = true;
            } else {
                $item[$key]['star'] = false;
            }
        }

        //循环$item，找出留言，存在tall里面
        foreach ($item as $key => $value) {
            $tall = $this->tallModel->where('postid', $value['id'])->with('user')->order('createtime desc')->select();
            if (!$tall) {
                $item[$key]['tall'] = [];
            } else {
                $item[$key]['tall'] = $tall;
            }
        }

        $data = [
            'count' => $count,
            'item' => $item,
        ];

        if ($item) {
            return $this->success('获取成功', '', $data);
        } else {
            return $this->error($this->PostsModel->getError());
        }
    }

    //关注的帖子列表
    public function getfollowposts()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            $id = $params['id'] ?? 0; //用户id
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 10;
            $userid = $params['userid'] ?? '';
        }

        if (empty($userid)) {
            return $this->error('暂没有关注');
        }

        $user = $this->UserModel->find($id);
        if (!$user) {
            return $this->error('用户不存在');
        }

        $count = $this->PostsModel->where('userid', 'in', $userid)->count();
        $item = $this->PostsModel->where('userid', 'in', $userid)->with('user')->order('createtime desc')->page($page, $limit)->select();

        //循环$item，判断是否点赞过，是则在数组中添加is_like字段，值为1，否则为0
        foreach ($item as $key => $value) {
            $arr = explode(',', $value['likes']);
            if (in_array($id, $arr)) {
                $item[$key]['is_like'] = true;
            } else {
                $item[$key]['is_like'] = false;
            }
        }

        // 循环$item，判断是否收藏过，是则在数组中添加is_collect字段，值为1，否则为0
        foreach ($item as $key => $value) {
            $res = model('common/User/Star')->where('userid', $id)->where('postid', $value['id'])->find();
            if ($res) {
                $item[$key]['star'] = true;
            } else {
                $item[$key]['star'] = false;
            }
        }

        //循环$item，找出留言，存在tall里面
        foreach ($item as $key => $value) {
            $tall = $this->tallModel->where('postid', $value['id'])->with('user')->order('createtime desc')->select();
            if (!$tall) {
                $item[$key]['tall'] = [];
            } else {
                $item[$key]['tall'] = $tall;
            }
        }

        $data = [
            'count' => $count,
            'item' => $item,
        ];

        if ($item) {
            return $this->success('获取成功', '', $data);
        } else {
            return $this->error($this->PostsModel->getError());
        }
    }

    //点赞
    public function good()
    {
        $id = $this->request->param('id', 0, 'trim');
        $userid = $this->request->param('userid', 0, 'trim');

        $user = $this->UserModel->find($userid);
        if (!$user) {
            $this->error('用户不存在');
        }

        $posts = $this->PostsModel->find($id);
        if (!$posts) {
            $this->error('帖子不存在');
        }

        //判断是否点赞过，是则取消点赞，否则点赞
        $arr = explode(',', $posts['likes']);

        //过滤空数组
        $arr = array_filter($arr);

        if (in_array($userid, $arr)) {
            //找出数组中$userid的下标
            $index = array_search($userid, $arr);
            //删除数组中$userid的下标
            unset($arr[$index]);

            $params = [
                'likes' => implode(',', $arr),
                'id' => $id,
            ];

            $res = $this->PostsModel->update($params);

            if ($res) {
                return $this->success('取消点赞');
            } else {
                return $this->error($this->PostsModel->getError());
            }
        } else {
            $arr[] = $userid;
            $params = [
                'likes' => implode(',', $arr),
                'id' => $id,
            ];
            $res = $this->PostsModel->update($params);
            if ($res) {
                return $this->success('点赞成功');
            } else {
                return $this->error($this->PostsModel->getError());
            }
        }
    }

    //收藏
    public function star()
    {
        $id = $this->request->param('id', 0, 'trim');
        $userid = $this->request->param('userid', 0, 'trim');

        $user = $this->UserModel->find($userid);
        if (!$user) {
            $this->error('用户不存在');
        }

        $posts = $this->PostsModel->find($id);
        if (!$posts) {
            $this->error('帖子不存在');
        }

        //判断是否收藏过，是则取消收藏，否则收藏
        $res = model('common/User/Star')->where('userid', $userid)->where('postid', $id)->find();

        if ($res) {
            $result = $this->StarModel->where('userid', $userid)->where('postid', $id)->delete();
            if ($result) {
                return $this->success('取消收藏');
            } else {
                return $this->error($this->PostsModel->getError());
            }
        } else {
            $params = [
                'userid' => $userid,
                'postid' => $id,
            ];
            $result = $this->StarModel->save($params);
            if ($result) {
                return $this->success('收藏成功');
            } else {
                return $this->error($this->PostsModel->getError());
            }
        }
    }

    //发布帖子
    public function addpost()
    {
        if ($this->request->isPost()) {
            // 获取普通表单字段
            $userid = $this->request->param('userid', 0, 'trim');
            $schoolid = $this->request->param('schoolid', 0, 'trim');
            $title = $this->request->param('title', '', 'trim');
            $content = $this->request->param('content', '', 'trim');

            // 获取文件数据
            $files = $this->request->file('imglist', '', 'trim');

            if (empty($title) && empty($content) && empty($files)) {
                return $this->error('请填写标题和内容');
            }

            $user = $this->UserModel->find($userid);
            if (!$user) {
                return $this->error('用户不存在');
            }

            $school = model('common/School/School')->find($schoolid);
            if (!$school) {
                return $this->error('学校不存在');
            }

            $params = [
                'userid' => $userid,
                'schoolid' => $schoolid,
                'title' => $title,
                'content' => $content,
            ];

            // 处理文件数据
            if (isset($_FILES['imglist']) && $_FILES['imglist']['size'] > 0) {
                $imgs = build_uploads('imglist');

                if ($imgs['code'] == 1) {
                    $params['thumbs'] = $imgs['data'];
                }
            }
            $res = $this->PostsModel->save($params);

            if ($res) {
                return $this->success('发布成功');
            } else {
                return $this->error($this->PostsModel->getError());
            }
        }
    }

    //发送留言
    public function sendtall()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0, 'trim');
            $postid = $this->request->param('postid', 0, 'trim');
            $title = $this->request->param('title', '', 'trim');

            $user = $this->UserModel->find($userid);
            if (!$user) {
                return $this->error('用户不存在');
            }

            $posts = $this->PostsModel->find($postid);
            if (!$posts) {
                return $this->error('帖子不存在');
            }

            if (empty($title)) {
                return $this->error('请填写留言内容');
            }

            $params = [
                'userid' => $userid,
                'postid' => $postid,
                'title' => $title,
            ];

            $res = $this->tallModel->save($params);

            if ($res) {
                return $this->success('发送成功');
            } else {
                return $this->error($this->tallModel->getError());
            }
        }
    }

    //获取更新留言
    public function gettall()
    {
        $postid = $this->request->param('postid', 0, 'trim');
        $userid = $this->request->param('userid', 0, 'trim');

        $user = $this->UserModel->find($userid);
        if (!$user) {
            return $this->error('用户不存在');
        }

        $posts = $this->PostsModel->find($postid);
        if (!$posts) {
            return $this->error('帖子不存在');
        }

        $tall = $this->tallModel->where('postid', $postid)->with('user')->order('createtime desc')->select();

        if ($tall) {
            return $this->success('获取成功', '', $tall);
        } else {
            return $this->error($this->tallModel->getError());
        }
    }
}
